package com.wenwu.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsOrganization;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsOrganizationMapper;
import com.wenwu.service.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService {

	@Autowired
	RelicsOrganizationMapper relicsOrganizationMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	public List<RelicsOrganization> selectAll() throws RelicsException {
		List<RelicsOrganization> list = null;
		try {
			list = relicsOrganizationMapper.selectAll();
		} catch (SQLException e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		} catch (Exception e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		}
		return list;
	}
	public RelicsOrganization selectByKey(Integer id) throws RelicsException {
		RelicsOrganization rl = null;
		try {
			rl = relicsOrganizationMapper.selectByPrimaryKey(id);
		} catch (SQLException e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		} catch (Exception e) {
			throw new RelicsException("对不起服务器异常请稍后重试！");
		}
		return rl;
	}
	
	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsOrganizationMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}
	
	public String addObj(RelicsOrganization organization)throws RelicsException{
		try{
			int result = 0;
			if (organization.getOrganId() == null) {
				result = relicsOrganizationMapper.insertSelective(organization);
				if (result > 0) {
					return "添加机构成功";
				} else {
					return "添加机构失败";
				}
			} else {
				result = relicsOrganizationMapper
						.updateByPrimaryKeySelective(organization);
				if (result > 0) {
					return "修改机构成功";
				} else {
					return "修改机构失败";
				}
			}
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}
	public int selectTotalRecord(RelicsOrganization organization)
			throws RelicsException {
		try {
			return relicsOrganizationMapper.selectTotalRecord(organization);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public List<RelicsOrganization> selectList(RelicsOrganization organization,
			PaginationVO pageVo) throws RelicsException {
		try {
			return relicsOrganizationMapper.selectList(organization, pageVo);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public List<RelicsOrganization> selectAllOrganization()throws Exception {
		try {
			return relicsOrganizationMapper.selectByExample(null);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
}
